package com.zaydanwu.cla.service.Impl;

import com.zaydanwu.cla.dao.StudentsMapper;
import com.zaydanwu.cla.model.Students;
import com.zaydanwu.cla.service.StudentsService;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import javax.annotation.Resource;

/**
 * @BelongsProject: cla
 * @BelongsPackage: com.zaydanwu.cla.service.Impl
 * @Author: zaydanwu
 * @CreateTime: 2018-12-10 23:07
 * @Description:
 */
@Service
public class StudentsServiceImpl implements StudentsService {

    @Resource
    private StudentsMapper studentsMapper;

    @Override
    public Students getStudentMessage(String name) {
        return studentsMapper.selectStudent(name);
    }

    @Override
    public Students getStudentMessageByStuNo(Long stuNo) {
        return studentsMapper.selectStudentByStuNo(stuNo);
    }

    @Override
    public int saveStudentMessage(Students students) {
        return studentsMapper.insert(students);
    }

    public Students getStudentMessage(Long stuNo, String password) {
        return studentsMapper.selectStudentByNameAndPwd(stuNo, password);
    }

    @Override
    public int updatePassword(Long stuNo, String phoneNumber, String newPassword) {
        Students students = studentsMapper.selectStudentByStuNo(stuNo);
        String phoneNumber1 = students.getPhoneNumber();
        if (phoneNumber.equals(phoneNumber1)) {
            String md5Pwd = DigestUtils.md5DigestAsHex(newPassword.getBytes());
            students.setPassword(md5Pwd);
        }
        return studentsMapper.updateByPrimaryKey(students);
    }
}
